package com.kk.xhr.model.service.impl;

import com.kk.xhr.common.pojo.ApplyStatusEnum;
import com.kk.xhr.model.assembler.LeaveStructKit;
import com.kk.xhr.model.dao.LeaveTimeDynamicSqlSupport;
import com.kk.xhr.model.dao.LeaveTimeMapper;
import com.kk.xhr.model.dto.ApplyQueryRequestDTO;
import com.kk.xhr.model.dto.LeaveDTO;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.entity.LeaveTime;
import com.kk.xhr.model.service.ILeaveService;
import lombok.RequiredArgsConstructor;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.stream.Collectors;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualToWhenPresent;
import static org.mybatis.dynamic.sql.SqlBuilder.isGreaterThanOrEqualToWhenPresent;
import static org.mybatis.dynamic.sql.SqlBuilder.isLessThanOrEqualToWhenPresent;

/**
 * 请假服务接口实现
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@Repository
@RequiredArgsConstructor
public class LeaveServiceImpl implements ILeaveService {
    private final LeaveTimeMapper leaveMapper;
    private final LeaveStructKit instance = LeaveStructKit.INSTANCE;

    /**
     * {@inheritDoc}
     */
    @Override
    public PageDto<LeaveDTO> query(ApplyQueryRequestDTO dto) {
        long count = leaveMapper.count(dsl -> applyQueryArgs(dsl.where(), dto));
        if (0 == count) {
            return PageDto.empty();
        }
        return new PageDto<>(count, leaveMapper.select(dsl -> applyQueryArgs(dsl.where(), dto)
            .orderBy(LeaveTimeDynamicSqlSupport.id.descending())
            .limit(dto.getLimit())
            .offset(dto.getOffset()))
            .stream()
            .map(instance::toDto)
            .collect(Collectors.toList()));
    }

    private <T extends AbstractWhereDSL<T>> T applyQueryArgs(T where, ApplyQueryRequestDTO dto) {
        where.and(LeaveTimeDynamicSqlSupport.userId, isEqualToWhenPresent(dto.getUserId()))
            .and(LeaveTimeDynamicSqlSupport.reviewerId, isEqualToWhenPresent(dto.getReviewerId()))
            .and(LeaveTimeDynamicSqlSupport.created, isGreaterThanOrEqualToWhenPresent(dto.getBegin()))
            .and(LeaveTimeDynamicSqlSupport.created, isLessThanOrEqualToWhenPresent(dto.getEnd()));
        if (null != dto.getStatus()) {
            where.and(LeaveTimeDynamicSqlSupport.status, isEqualTo(dto.getStatus().getValue()));
        }
        return where;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean add(LeaveDTO dto) {
        return 1 == leaveMapper.insertSelective(instance.toEntity(dto));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean modify(Long id, ApplyStatusEnum status) {
        if (null == status) {
            return false;
        }
        return 1 == leaveMapper.updateByPrimaryKeySelective(new LeaveTime().withId(id).withStatus(status.getValue()));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Optional<LeaveDTO> queryById(Long id) {
        return leaveMapper.selectByPrimaryKey(id).map(instance::toDto);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Optional<LeaveDTO> query(Long id) {
        return leaveMapper.selectOne(dsl -> dsl
            .where(LeaveTimeDynamicSqlSupport.id, isEqualTo(id))
            .and(LeaveTimeDynamicSqlSupport.status, isEqualTo(ApplyStatusEnum.APPLYING.getValue())))
            .map(LeaveStructKit.INSTANCE::toDto);
    }
}
